package com.fgxia.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fgxia.admin.service.IAdminAppIndexService;
import com.ruoyi.business.domain.FreightInfo;
import com.ruoyi.business.domain.vo.admin.AdminCountOrderVo;
import com.ruoyi.business.mapper.FreightInfoMapper;
import com.ruoyi.business.mapper.OrderInfoMapper;
import com.ruoyi.common.enums.FreightStatus;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.enums.SysYesNo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;

@RequiredArgsConstructor
@Service
public class AdminAppIndexServiceImpl implements IAdminAppIndexService {

    private final OrderInfoMapper orderInfoMapper;
    private final FreightInfoMapper freightInfoMapper;

    /**
     * 获取各类型订单数量
     *
     * @return
     */
    @Override
    public AdminCountOrderVo countOrder() {
        AdminCountOrderVo orderCountVo = new AdminCountOrderVo();
        orderCountVo.setNewOrderCount(
            orderInfoMapper.countByStatus(Collections.singletonList(OrderStatus.INITIALIZED.getCode()))
        );
        orderCountVo.setOrderCount(
            orderInfoMapper.countByStatus(CollUtil.newArrayList(OrderStatus.WAITING_FOR_PICKUP.getCode(), OrderStatus.DELIVERING.getCode()))
        );
        orderCountVo.setWaybillCount(
            freightInfoMapper.countByStatus(CollUtil.newArrayList(FreightStatus.WAITING_FOR_PICKUP.getCode(), FreightStatus.DELIVERING.getCode()))
        );
        orderCountVo.setReceiptCount(
            freightInfoMapper.countByStatus(Collections.singletonList(FreightStatus.WAITING_FOR_RECEIPT.getCode()))
        );
        orderCountVo.setPaymentOrderCount(
            orderInfoMapper.countByStatus(Collections.singletonList(OrderStatus.WAITING_FOR_PAYMENT.getCode()))
        );
        orderCountVo.setPaymentCount(
            freightInfoMapper.countByStatus(Collections.singletonList(FreightStatus.WAITING_FOR_PAYMENT.getCode()))
        );
        orderCountVo.setUrgentCount(
            freightInfoMapper.selectCount(new QueryWrapper<FreightInfo>().eq("is_urgency", SysYesNo.Y.name()))
        );
        return orderCountVo;
    }
}
